package com.multi.thread.chapter07.demo05;

public class MyThread extends Thread {
	
	private Object lock;
	private String showChar;
	private int showNumPosition;
	private int printCount = 0;
	volatile private static int addNumber = 1;
	
	public MyThread(Object lock, String showChar, int showNumPosition) {
		super();
		this.lock = lock;
		this.showChar = showChar;
		this.showNumPosition = showNumPosition;
	}
	
	@Override
	public void run() {
		try {
			synchronized (lock) {
				while (true) {
					if (addNumber % 2 == showNumPosition) {
						System.out.println("ThreadName = " + Thread.currentThread().getName() 
								+ ", runCount = " + addNumber + " " + showChar);
						lock.notifyAll();
						addNumber++;
						printCount++;
						if (printCount == 2)
							break;
					} else {
						lock.wait();
					}
				}
			}
		} catch (InterruptedException e) { 
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		Object lock = new Object();
		MyThread a = new MyThread(lock, "A", 1);
		MyThread c = new MyThread(lock, "B", 0);
		a.start();
		c.start();
	}

}
